import utils from '../../utils/util.js'
Page({
  // 导入
  ...utils.func,
  // 页面初始数据
  data: {
    userInfo: '',
    openid: '',
    appKeys: 0,
    userTotal: 0,
    makeWrongTotal: 0,
    msgTotal: 0
  },
  // 监听页面加载
  onLoad: function (options) {

  },
  // 监听页面显示
  onShow: function () {
    
    this.getUser()
    this.getUserTotal()
    this.getKeysTotal()
  },
  // 清理缓存
  clearCache() {
    wx.clearStorage();
    wx.showToast({
      title: '清理成功',
      icon: 'none',
      duration: 2000
    })
  },
  gologin() {
    var th=this;
    console.log("登录")
    wx.getUserProfile({
      desc: '用于完善会员资料', 
      // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          oppenid: res.cloudID
        })
        let that = th;
    wx.cloud.callFunction({
      name: 'getUser',
    }).then(res => {
      console.log(res)
      if (res.result.data.length == 0) {
        console.log("hubu")
        wx.cloud.callFunction({
          name: 'addUser',
          data: {
            nickName: that.data.userInfo.nickName,
            gender: that.data.userInfo.gender,
            language: that.data.userInfo.language,
            city: that.data.userInfo.city,
            province: that.data.userInfo.province,
            country: that.data.userInfo.country,
            avatarUrl: that.data.userInfo.avatarUrl
          },
    
        }).then(addres => {
          console.log(addres)
          that.setData({
            userInfo: that.data.userInfo
          })
          wx.setStorageSync('userInfo', JSON.stringify(that.data.userInfo))
        })
      } else {
        console.log(res.result)
        that.setData({
          userInfo: res.result.data[0]
        })
        wx.setStorageSync('userInfo', JSON.stringify(res.result.data[0]))
      }
    })
      },
      fail: (res)=>{
        console.log(res)
      }
    })
  },
  // 提示
  godev() {
    wx.showToast({
      title: '功能开发中',
      icon: 'none',
      duration: 2000
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "口袋工具箱-一个多功能工具箱",
      path: 'pages/index/index'
    };
  },

  // 获取用户总数
  getUserTotal() {
    let that = this
    wx.cloud.callFunction({
      name: 'getUserTotal',
      data: {}
    }).then(res => {
      that.setData({
        userTotal: res.result.total
      })
    })
  },
  getKeysTotal() {
    let that = this
    wx.cloud.callFunction({
      name: 'getKeysTotal',
      data: {}
    }).then(res => {
      that.setData({
        appKeys: res.result.total
      })
    })
  },

  // 获取当前用户
  getUser() {

    let that = this
    wx.cloud.callFunction({
      name: 'getUser',
    }).then(res => {
      if (res.result.data != null) {
        that.setData({
          userInfo: res.result.data[0]
        })
        wx.setStorageSync('userInfo', JSON.stringify(res.result.data[0]))
      } else { }
    })
  },

  loginout(){
    wx.showModal({
      title: "退出登录",
      content: "您确定要退出登录吗",
      success: res=>{
        if (res.confirm) {
          this.setData({
            userInfo: ''
          })
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  }

})